﻿using System.Collections.Generic;

namespace IceCS.DesignPattern.Core.ObserverPattern.NumberChange
{
    public class Subject
    {
        private readonly List<Observer> _observers = new List<Observer>();

        private int _state;

        public int State
        {
            get => _state;
            set
            {
                _state = value;
                NotifyAllObservers();
            }

        }

        public void AddObserver(Observer observer)
        {
            observer.Subject = this;
            _observers.Add(observer);
        }

        public void NotifyAllObservers() => _observers.ForEach(o => o.Update());
    }
}
